package dync.rtmpc_demo.hoster;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.anyrtc.rtmpc_hybrid.RTMPCHosterHelper;
import org.anyrtc.rtmpc_hybrid.RTMPCHosterKit;
import org.anyrtc.rtmpc_hybrid.RTMPCHybird;
import org.anyrtc.rtmpc_hybrid.RTMPCVideoView;
import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.RendererCommon;
import org.webrtc.VideoRenderer;

import butterknife.BindView;
import dync.rtmpc_demo.BaseActivity;
import dync.rtmpc_demo.R;
import dync.rtmpc_demo.utils.Constans;
import dync.rtmpc_demo.utils.CustomDialog;
import dync.rtmpc_demo.utils.RTMPCHttpSDK;

public class HosterActivity extends BaseActivity implements RTMPCVideoView.BtnVideoCloseEvent,RTMPCHosterHelper {
    @BindView(R.id.OnRtmpStreamOK)
    TextView OnRtmpStreamOK;
    @BindView(R.id.OnRtmpStreamReconnecting)
    TextView OnRtmpStreamReconnecting;
    @BindView(R.id.OnRtmpStreamStatus)
    TextView OnRtmpStreamStatus;
    @BindView(R.id.OnRtmpStreamFailed)
    TextView OnRtmpStreamFailed;
    @BindView(R.id.OnRtmpStreamClosed)
    TextView OnRtmpStreamClosed;
    @BindView(R.id.OnRtmpAudioLevel)
    TextView OnRtmpAudioLevel;
    @BindView(R.id.OnRTCOpenLineResult)
    TextView OnRTCOpenLineResult;
    @BindView(R.id.OnRTCApplyToLine)
    TextView OnRTCApplyToLine;
    @BindView(R.id.OnRTCCancelLine)
    TextView OnRTCCancelLine;
    @BindView(R.id.OnRTCLineClosed)
    TextView OnRTCLineClosed;
    @BindView(R.id.OnRTCOpenVideoRender)
    TextView OnRTCOpenVideoRender;
    @BindView(R.id.OnRTCCloseVideoRender)
    TextView OnRTCCloseVideoRender;
    @BindView(R.id.OnRTCOpenAudioLine)
    TextView OnRTCOpenAudioLine;
    @BindView(R.id.OnRTCCloseAudioLine)
    TextView OnRTCCloseAudioLine;
    @BindView(R.id.OnRTCUserMessage)
    TextView OnRTCUserMessage;
    @BindView(R.id.OnRTCUserBarrage)
    TextView OnRTCUserBarrage;
    @BindView(R.id.OnRTCMemberListWillUpdate)
    TextView OnRTCMemberListWillUpdate;
    @BindView(R.id.OnRTCMember)
    TextView OnRTCMember;
    @BindView(R.id.OnRTCMemberListUpdateDone)
    TextView OnRTCMemberListUpdateDone;
    @BindView(R.id.rl_hoster_video)
    RelativeLayout rlHosterVideo;
    private RTMPCHosterKit mHosterKit;
    private RTMPCVideoView mVideoView;
    CustomDialog customDialog;
    public String gurstPeerid="";
    String randomString;
    @Override
    public int getLayoutId() {
        return R.layout.activity_hoster;
    }


    @Override
    public void initView(Bundle savedInstanceState) {
        RTMPCHybird.Inst().SetScreenToLandscape();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        mVideoView = new RTMPCVideoView(rlHosterVideo, RTMPCHybird.Inst().Egl(), true,false);
        mVideoView.setBtnCloseEvent(this);
        mHosterKit = new RTMPCHosterKit(this,this);
        mHosterKit.SetVideoCapturer(mVideoView.OnRtcOpenLocalRender(RendererCommon.ScalingType.SCALE_ASPECT_FIT).GetRenderPointer(), true);
        mHosterKit.SetNetAdjustMode(RTMPCHosterKit.RTMPNetAdjustMode.RTMP_NA_Fast);
        mHosterKit.SetVideoMode(RTMPCHosterKit.RTMPVideoMode.RTMPC_Video_QHD);
//        mHosterKit.StartPushRtmpStream(Constans.RTMP_PUSH_URL);
        mHosterKit.StartPushRtmpStream("rtmp://live.hkstv.hk.lxdns.com/live/hks");
        randomString = RTMPCHttpSDK.getRandomString(12);
        mHosterKit.OpenRTCLine(randomString, "hostID", getUserData());

    }
    public String getUserData(){
        String rtmpPushUrl = String.format(Constans.RTMP_PUSH_URL, randomString);
        String rtmpPullUrl = String.format(Constans.RTMP_PULL_URL, randomString);
        String hlsUrl = String.format(Constans.HLS_URL, randomString);
        JSONObject item = new JSONObject();
        try {
            item.put("hosterId", "Loka");
            item.put("rtmp_url", "rtmp://live.hkstv.hk.lxdns.com/live/hks");
            item.put("hls_url", hlsUrl);
            item.put("topic", "测试活动");
            item.put("anyrtcId", randomString);
            item.put("isAudioOnly", false);
            item.put("screen_mode", "");
            item.put("isVideoAudioLiving", false);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return item.toString();
    }
    @Override
    public void CloseVideoRender(View view, String strPeerId) {
        mHosterKit.HangupRTCLine(strPeerId);
    }

    @Override
    public void OnSwitchCamera(View view) {
        mHosterKit.SwitchCamera();
    }

    @Override
    public void OnRtmpStreamOK() {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpStreamOK!=null) {
                    OnRtmpStreamOK.setText("RTMP连接成功");
                }
            }
        });
    }

    @Override
    public void OnRtmpStreamReconnecting(final int i) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpStreamReconnecting!=null) {
                    OnRtmpStreamReconnecting.setText("RTMP重连次数 " + i);
                }
            }
        });
    }

    @Override
    public void OnRtmpStreamStatus(final int i, final int i1) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpStreamStatus!=null) {
                    OnRtmpStreamStatus.setText("RTMP流状态 \n" + "延迟时间：" + i + "\n上行带宽：" + i1);
                }
            }
        });
    }

    @Override
    public void OnRtmpStreamFailed(final int i) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpStreamFailed!=null) {
                    OnRtmpStreamFailed.setText("RTMP推流失败 \n" + "错误码：" + i);
                }
            }
        });
    }

    @Override
    public void OnRtmpStreamClosed() {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpStreamClosed!=null) {
                    OnRtmpStreamClosed.setText("RTMP推流关闭");
                }
            }
        });
    }

    @Override
    public void OnRtmpAudioLevel(final String s, final int i) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRtmpAudioLevel!=null) {
                    OnRtmpAudioLevel.setText("用户id:" + s + "\n音量等级：" + i);
                }
            }
        });
    }

    @Override
    public void OnRTCOpenLineResult(final int i, final String s) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRTCOpenLineResult!=null) {
                    if (i == 0) {
                        OnRTCOpenLineResult.setText("RTC服务开启成功");
                    } else {
                        OnRTCOpenLineResult.setText("RTC服务开启失败+\n错误码" + i + "原因\n：" + s);
                    }
                }
            }
        });

    }

    @Override
    public void OnRTCApplyToLine(final String s, final String s1, final String s2) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRTCApplyToLine!=null) {
                    OnRTCApplyToLine.setText("有游客申请连麦\n用户Id:" + s1);
                }
                gurstPeerid=s;
                showApplyLineDialog(s2);
            }
        });
    }

    @Override
    public void OnRTCCancelLine(final String s) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRTCCancelLine!=null) {
                    OnRTCCancelLine.setText("无人连麦");
                    OnRTCCancelLine.setText("游客取消申请连麦\n用户Id:" + s);
                }
                gurstPeerid="";
                if (customDialog!=null){
                    customDialog.dismiss();
                }
            }
        });
    }

    @Override
    public void OnRTCLineClosed(int i, String s) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRTCLineClosed!=null) {
                    OnRTCLineClosed.setText("RTC服务关闭");
                }
            }
        });
    }

    @Override
    public void OnRTCOpenVideoRender(final String s, String s1) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final VideoRenderer render = mVideoView.OnRtcOpenRemoteRender(s, RendererCommon.ScalingType.SCALE_ASPECT_FIT);
                if (null != render) {
                    if (mHosterKit != null) {
                        mHosterKit.SetRTCVideoRender(s, render.GetRenderPointer());
                    }

                }
            }
        });
    }

    @Override
    public void OnRTCCloseVideoRender(final String s, String s1) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mHosterKit != null && mVideoView != null) {
                    mHosterKit.SetRTCVideoRender(s, 0);
                    mVideoView.OnRtcRemoveRemoteRender(s);
                }
            }
        });
    }

    @Override
    public void OnRTCOpenAudioLine(String s, String s1) {
        HosterActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (OnRTCOpenAudioLine!=null) {
                }
            }
        });
    }

    @Override
    public void OnRTCCloseAudioLine(String s, String s1) {

    }

    @Override
    public void OnRTCUserMessage(String s, String s1, String s2, String s3) {

    }

    @Override
    public void OnRTCUserBarrage(String s, String s1, String s2, String s3) {

    }

    @Override
    public void OnRTCMemberListWillUpdate(int i) {

    }

    @Override
    public void OnRTCMember(String s, String s1) {

    }

    @Override
    public void OnRTCMemberListUpdateDone() {

    }

    private void showApplyLineDialog(final String name){
        CustomDialog.Builder builder = new CustomDialog.Builder(this);
        customDialog = builder.setContentView(R.layout.dialog_base_layout_two_btn)
                .setCancelable(false)
                .setGravity(Gravity.CENTER)
                .setAnimId(R.style.dialog_live_style)
                .setBackgroundDrawable(true)
                .show(new CustomDialog.Builder.onInitListener() {
                    @Override
                    public void init(CustomDialog customDialog) {
                        TextView content = (TextView) customDialog.findViewById(R.id.content);
                        content.setText(name+"申请连麦");
                        TextView tv_ok= (TextView) customDialog.findViewById(R.id.tv_ok);
                        TextView tv_no= (TextView) customDialog.findViewById(R.id.tv_no);
                        tv_ok.setOnClickListener(new ApplyLineOnclick());
                        tv_no.setOnClickListener(new ApplyLineOnclick());
                    }
                });
    }


    public class ApplyLineOnclick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.tv_no:
                    mHosterKit.RejectRTCLine(gurstPeerid,true);
                    customDialog.dismiss();
                    break;
                case R.id.tv_ok:
                    mHosterKit.AcceptRTCLine(gurstPeerid);
                    customDialog.dismiss();
                    break;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHosterKit != null) {
            mHosterKit.StopRtmpStream();
            mVideoView.OnRtcRemoveLocalRender();
            mHosterKit.Clear();
            mHosterKit = null;
        }
    }
}
